<?php

namespace app\api\controller;

use app\common\model\Users;
use think\exception\ValidateException;
use think\facade\Db;
use think\facade\Filesystem;

class Index extends Base
{


    //分类图标库
    public function iconList()
    {
        $list = Db::name('icon_cate')->select();
        $data = [];
        foreach ($list as $item) {
            $t = [
                "title" => $item['icon_cate_name'],
                "list" => Db::name("icon_list")->where('icon_cate_id', $item['id'])
                    ->column('img')
            ];
            $data[] = $t;
        }
        $this->insertResponse("icon_list", $data);
        return $this->response('请求成功');
    }

    //用户图像库
    public function avatar()
    {
        $avatar_list = Db::name("user_avatar")
            ->column('avatar');
        $this->insertResponse('list', $avatar_list);
        return $this->response('请求成功');
    }

    //首页
    public function index()
    {
        $data = $this->requestData;
        $this->insertResponse('', $data);
        return $this->response("请求成功");
    }


    public function upimage()
    {
        // 获取上传文件表单字段名
        $fileKey = array_keys(request()->file());
        // 获取表单上传文件
        $file = request()->file($fileKey['0']);
        try {
            validate(['image' => 'filesize:' . config('upload.imageMaxSize') . '|fileExt:jpg,png,gif,jpeg'])
                ->check(object2array($file));
            $savename = Filesystem::disk('public')->putFile('bank_list', $file);
            $path = str_replace('\\', '/', $savename);
            $hex = checkHex(public_path() . $path);
            if ($hex == 1) {
                return $this->ajaxError('请上传合法图片');
            }
            return $this->ajaxSuccess(['url' => tomedia('/' . $path)]);
        } catch (ValidateException $e) {
            return $this->ajaxError($e->getMessage());
        }
    }

    public function content($type)
    {
        if (!in_array($type, ['treaty', 'about', 'secret'])) {
            return $this->ajaxError('参数错误');
        }
        $content = get_sale_setting($type);
        return $this->ajaxSuccess(['detail' => $content, 'img_base_url' => request()->domain(true)]);
    }


}